//
//  NvEditMusicViewController.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/7/2.
//  Copyright © 2018年 meishe. All rights reserved.
//

#import "NvEditMusicViewController.h"
#import "NvAddCaptionView.h"
#import "NvsTimelineCaption.h"
#import "NvItemButton.h"
#import "NvSelectMusicViewController.h"
#import "NvsAudioTrack.h"
#import "NvTimelineUtils.h"
#import "NvTimelineData.h"
#import "MSSysAlertUtil.h"
#import "NvTipsView.h"
#import "NvMultiMusicViewController.h"

@interface NvEditMusicViewController ()<NvsStreamingContextDelegate, NvLiveWindowPanelViewDelegate> {
    BOOL isPlay;
}

@property (nonatomic, strong) NvAddCaptionView *addCaptionView;
@property (nonatomic, strong) NvItemButton *singleItemButton;
@property (nonatomic, strong) NvItemButton *multiItemButton;
@property (nonatomic, strong) NvMusicInfoModel *musicInfo;
@property (nonatomic, strong) NvTimelineData *timelineData;

@end

@implementation NvEditMusicViewController

-(void)dealloc {
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"音乐";
    NvsTimeline *timeline = [NvTimelineUtils createTimeline:self.editMode];
    [NvTimelineUtils recreateTimeline:timeline];
    self.timelineData = [NvTimelineData sharedInstance];
    self.timeline = timeline;
    [self initSubViews];
    self.liveWindowPanel.delegate = self;
    [self.liveWindowPanel hiddenVolumeButton];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.streamingContext stop];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [NvTimelineUtils recreateTimeline:self.timeline];
    if (isPlay) {
        [self.liveWindowPanel playAtTime:0];
    }
}

- (void)leftNavButtonClick:(UIButton *)button {
    [self.streamingContext removeTimeline:self.timeline];
    [super leftNavButtonClick:button];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)singleItemClick:(NvItemButton *)itemButton {
    isPlay = NO;
    NvSelectMusicViewController *selectMusic = NvSelectMusicViewController.new;
    selectMusic.delegate = self;
    
    NSMutableArray *musicDataArray = [[NvTimelineData sharedInstance] musicDataArray];
    [musicDataArray removeAllObjects];
    
    [[NvTimelineData sharedInstance] setMusicDataArray:musicDataArray];
    
    [self.navigationController pushViewController:selectMusic animated:YES];
}

- (void)multiItemClick:(NvItemButton *)itemButton {
    NvMultiMusicViewController *vc = NvMultiMusicViewController.new;
    vc.editMode = self.editMode;
    vc.title = @"多段音乐";
    NSMutableArray *musicDataArray = [[NvTimelineData sharedInstance] musicDataArray];
    for (NvMusicInfoModel *musicInfo in musicDataArray) {
        if (musicInfo.isBGM) {
            musicInfo.isBGM = NO;
            musicInfo.outPoint = self.timeline.duration;
        }
    }
    [[NvTimelineData sharedInstance] setMusicDataArray:musicDataArray];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)toustTipView:(NSString *)title{
    NvTipsView *tipView = [[NvTipsView alloc]initWithFrame:CGRectMake(0, 0, SCREANWIDTH, SCREANHEIGHT) withPrompt:@"提示" describeTitle:[NSString stringWithFormat:@"Demo%@界面正在开发中，敬请期待。SDK内部已支持。",title] describeContent:nil buttonText:@"知道了" withCenter:YES];
    [tipView.clickBtn addTarget:self action:@selector(knowClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tipView];
}

#pragma mark tipView按钮点击事件
- (void)knowClick:(UIButton *)sender{
    [sender.superview.superview removeFromSuperview];
}

// MARK: initSubviews
- (void)initSubViews {
    self.singleItemButton = [NvItemButton buttonWithType:UIButtonTypeCustom];
    [self.singleItemButton setImage:[NvUtils imageWithName:@"NvEditMusic"] forState:UIControlStateNormal];
    [self.singleItemButton setTitle:@"单段音乐" forState:UIControlStateNormal];
    self.singleItemButton.titleLabel.textColor = [UIColor whiteColor];
    self.singleItemButton.titleLabel.font = [NvUtils fontWithSize:12];
    [self.view addSubview:self.singleItemButton];
    [self.singleItemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(110*SCREANSCALE));
        make.width.equalTo(@(70*SCREANSCALE));
        make.height.equalTo(@(80*SCREANSCALE));
        make.bottom.equalTo(@(-60*SCREANSCALEHEIGHT));
    }];
    [self.singleItemButton addTarget:self action:@selector(singleItemClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.multiItemButton = [NvItemButton buttonWithType:UIButtonTypeCustom];
    [self.multiItemButton setImage:[NvUtils imageWithName:@"NvEditMusic_1"] forState:UIControlStateNormal];
    [self.multiItemButton setTitle:@"多段音乐" forState:UIControlStateNormal];
    self.multiItemButton.titleLabel.textColor = [UIColor whiteColor];
    self.multiItemButton.titleLabel.font = [NvUtils fontWithSize:12];
    [self.view addSubview:self.multiItemButton];
    [self.multiItemButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-110*SCREANSCALE));
        make.width.equalTo(@(70*SCREANSCALE));
        make.height.equalTo(@(80*SCREANSCALE));
        make.bottom.equalTo(@(-60*SCREANSCALEHEIGHT));
    }];
    [self.multiItemButton addTarget:self action:@selector(multiItemClick:) forControlEvents:UIControlEventTouchUpInside];
}

// MARK: NvSelectMusicViewControllerDelegate
- (void)selectMusicViewController:(NvSelectMusicViewController *)selectMusicViewController withItem:(NvEditSelectMusicItem *)item trimIn:(float)trimIn trimOut:(float)trimOut {
    [[[NvTimelineData sharedInstance] musicDataArray] removeAllObjects];
    
    self.musicInfo = [NvMusicInfoModel new];
    self.musicInfo.musicPath = item.musicPath;
    self.musicInfo.trimIn = trimIn*NV_TIME_BASE;
    self.musicInfo.trimOut = trimOut*NV_TIME_BASE;
    self.musicInfo.musicName = item.musicName;
    self.musicInfo.volume = 1;
    self.musicInfo.isBGM = YES;
    [[[NvTimelineData sharedInstance] musicDataArray] addObject:self.musicInfo];
    [NvTimelineUtils resetMusicTrack:self.timeline musicDataArray:[[NvTimelineData sharedInstance] musicDataArray]];
    isPlay = YES;
}

- (void)selectNoneMusic {
    [[[NvTimelineData sharedInstance] musicDataArray] removeAllObjects];
    [NvTimelineUtils resetMusicTrack:self.timeline musicDataArray:[[NvTimelineData sharedInstance] musicDataArray]];
    isPlay = YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
